/**
 * 
 */
package com.xb.cloud.product.merchant.inner.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.merchant.inner.service.ShopNavigationGroupInnerService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopNavigationGroupBo;
import com.xb.cloud.product.merchant.service.impl.AbstractService;
import com.xb.cloud.product.service.business.model.ShopGroupSku;
import com.xb.cloud.product.service.business.model.ShopNavigationGroup;
import com.xb.cloud.product.service.business.model.ShopNavigationGroupExample;
import com.xb.cloud.product.service.business.model.ShopNavigationGroupExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.ShopGroupSkuMapper;
import com.xb.cloud.product.service.business.model.mapper.ShopNavigationGroupMapper;

/**
 * 
 * 商店内导航分组 管理服务类;
 * 
 * @author kexue
 *
 */
@Service
public class ShopNavigationGroupInnerServiceImpl extends AbstractService implements ShopNavigationGroupInnerService{

	private static final Logger LOGGER=LoggerFactory.getLogger(ShopNavigationGroupInnerServiceImpl.class);
	
	
	@Resource
	private ShopNavigationGroupMapper  shopNavigationGroupMapper;
	

	@Resource
	private ShopGroupSkuMapper  shopGroupSkuMapper;

	
	/**
	 * 将sku加入店铺多个分组中
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED,value="transactionManager_product")
	public int createShopNavicationGroupSkuRef(Long skuId, List<Long> groupIdList,
			EndMerchantUserBo endMerchantUserBo) {
		int insertCount=0;
		for (Long groupId : groupIdList) {
			ShopGroupSku groupSku= new ShopGroupSku();
			groupSku.setId(ipKeyGenerator.generateKey()); 
			groupSku.setSkuId(skuId);
			groupSku.setGroupId(groupId);
			groupSku.setShopId(endMerchantUserBo.getShopId());
			
			groupSku.setCreateTime(timeService.getDate());
			groupSku.setCreateUserId(endMerchantUserBo.getId());
			groupSku.setCreateUserName(endMerchantUserBo.getLoginName());
			int eleCount = shopGroupSkuMapper.insertSelective(groupSku);
			if (eleCount <= 0) {
				String message = "插入ShopGroup sku管理失败" + JsonUtils.beanToJson(groupSku);
				LOGGER.error(message);
				throw new RuntimeException("插入ShopGroup sku管理失败") ;
			}else{
				insertCount+=eleCount;
			}
			 
		}
		return insertCount;
	}

	/**
	 * 根据分组的id,获取分组对应的数据
	 * @param groupId
	 * @return
	 */
	public ShopNavigationGroupBo getShopNavGroupBoById(Long groupId){
		
		ShopNavigationGroupExample groupExample=new  ShopNavigationGroupExample() ; 
		Criteria groupCriteria = groupExample.createCriteria();
		groupCriteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		groupCriteria.andIsEnabledEqualTo(DbConstants.TRUE);
		groupCriteria.andIdEqualTo(groupId);
		List<ShopNavigationGroup> groupList = shopNavigationGroupMapper.selectByExample(groupExample); 
		if(groupList!= null && groupList.size()>0){
			ShopNavigationGroupBo groupBo = BeanUtils.newAndCopyProperties(ShopNavigationGroupBo.class, groupList.get(0));
			return groupBo;
		}
		return null;
	}
}
